// Copyright (C) 2011 YANG Hao (yangh2007@gmail.com). All rights reserved.
//
// This file is part of the (cute)octopus project.
//
// This file may be distributed and/or modified under the terms of the
// GNU General Public License version 2 as published by the Free Software
// Foundation.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Author(s): YANG Hao


#ifndef ODOC_H
#define ODOC_H

#include <QObject>
#include <QMutex>
#include <QFileInfo>

#include "ostd.h"
#include "ocreator.h"
#include "odescriptors.h"
#include "oapplication.h"

class OEvent;
class OTaskAsync;
class OWindow;

class OCTOPUS_EXPORT ODoc : public QObject
{
public:
	ODoc(const QString& filePath, const QIcon& icon = QIcon(O_DEVELOP_ICON), 
		QObject *parent = 0);// take ownership of the device
	~ODoc(){}

	inline bool tryLock() {return m_mutex.tryLock(100);}
	inline void lock() {m_mutex.lock();}
	inline void unlock() {m_mutex.unlock();}

	inline bool isReadOnly() const {return m_readOnly;}
	inline bool isModified() const {return m_modified;}	
	
	// async update
	bool updateWidgets(OTaskAsync* invoker = 0);

	void setModified(bool m);
	void setReadOnly(bool r);

	inline bool notExist() const {return !m_fileInfo.exists();}

	inline bool hasWindows() const {return !rel_winds.empty();}
	inline const QList<OWindow*> windows() const {return rel_winds;}
	inline OWindow* windowAt(int index) const {return rel_winds.at(index);}

	inline QString suffix() const {return m_fileInfo.suffix();}
	inline ODocDescriptor* format() const {return oCreator()->format(m_fileInfo.suffix());}
	inline QFileInfo fileInfo() const {return m_fileInfo;}
	inline QString filePath() const {return m_fileInfo.filePath();}
	
	void setResource(const QString& filePath);

	inline QIcon icon() const {return m_icon;}

protected:
	// source info
	QFileInfo m_fileInfo;
	
	QIcon m_icon;

	// widgets
	QList<OWindow*> rel_winds;

	bool m_waitingForWidgetUpdate;
	bool m_modified;
	bool m_readOnly;
	
	// TODO: ...
	QMutex m_mutex;

	friend class OWindow;
	friend class OTaskAsync;
	friend class OCreator;	
};

typedef QList<ODoc*> ODocList;

//OCTOPUS_EXPORT ODocList oRequireDocs(ODoc* current, const QStringList& newDocDescriptors);


#endif // ODOC_H
